package lws.com.liwushuo.view.fragment.shouye;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.List;

import lws.com.liwushuo.R;
import lws.com.liwushuo.adapter.shouye.JinxuanListViewAdapter;
import lws.com.liwushuo.bean.shouye.JinxuanLiwuBean;
import lws.com.liwushuo.bean.shouye.JinxuanViewPagerBean;
import lws.com.liwushuo.server.shouye.ShouyeServer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class JinxuanViepagerActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, Callback<JinxuanViewPagerBean>, View.OnClickListener {


    private ListView listView;
    private JinxuanListViewAdapter listViewAdapter;
    private TextView title;
    private ImageView backImg;
    private ImageView fenxiang;
    private int collectionId;
    private ShouyeServer server;
    private int limit=20;
    private int offset=0;
    private WindowManager.LayoutParams params;
    private PopupWindow sharePopupWindow;
    private LinearLayout rootView;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shouye_jinxuan_viewpagerclick_fragment);
        //获取id
        collectionId = getIntent().getIntExtra("collectionId", -1);
        //状态栏设置为黑色
        setStatusBar();
        initView();
        //请求网络
        server = new Retrofit.Builder().baseUrl("http://api.liwushuo.com/").addConverterFactory(GsonConverterFactory.create()).build().create(ShouyeServer.class);
        server.getViewpagerBean(collectionId,limit,offset).enqueue(this);
    }


    /**
     * 初始化控件
     */
    private void initView() {
        if (listView == null) {
            //详情礼物列表
            listView = ((ListView)findViewById(R.id.shouye_jinxuan_viewpager_listView));
            //listView的适配器
            listViewAdapter = new JinxuanListViewAdapter(this);
            listView.setAdapter(listViewAdapter);//空数据
            //item点击事件
            listView.setOnItemClickListener(this);
        }
        title = ((TextView) findViewById(R.id.shouye_jinxuan_viewpagerclick_title));
        backImg = ((ImageView) findViewById(R.id.shouye_jinxuan_viewpagerclick_back));
        backImg.setOnClickListener(this);//顶部返回的监听
        fenxiang = ((ImageView) findViewById(R.id.shouye_jinxuan_viewpagerclick_fenxiang));
        fenxiang.setOnClickListener(this);//分享的监听
        //窗体的参数
        params = getWindow().getAttributes();
        initPopupWindow();//初始化popupwindow
        //父窗体
        rootView = (LinearLayout) findViewById(R.id.viewpagerDetial_rootView);
    }

    /**
     * 初始化分享的弹窗控件
     */
    public void initPopupWindow() {
        sharePopupWindow = new PopupWindow();
        View popupWindowView = LayoutInflater.from(this).inflate(R.layout.shouye_gonglue_share_layout, null);
        popupWindowView.findViewById(R.id.shouye_gonglue_share_cancle).setOnClickListener(this);
        sharePopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        sharePopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        sharePopupWindow.setContentView(popupWindowView);
        //点击外部时，关闭popupwindon
        sharePopupWindow.setFocusable(true);
        sharePopupWindow.setOutsideTouchable(true);
        sharePopupWindow.setBackgroundDrawable(getResources().getDrawable(R.color.WhiteBackgroud));
        //给弹窗设置动画
        sharePopupWindow.setAnimationStyle(R.style.gonglue_share_anim);
        //设置关闭时的监听
        sharePopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                //关闭的时候将外部透明度改回来
                params.alpha = 1.0f;
                getWindow().setAttributes(params);
            }
        });

    }



    /**
     * 设置状态栏颜色
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void setStatusBar() {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getResources().getColor(R.color.black));//最低版本要21也就时5.0以上
    }


    /**
     * 礼物列表的点击事件，
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        JinxuanLiwuBean.DataBean.ItemsBean itemsBean = (JinxuanLiwuBean.DataBean.ItemsBean) listViewAdapter.getItem(position);
        //获取攻略详情页面的数据请求id，以及查看全集的请求id，然后跳转过去
        int columnId = itemsBean.getColumn().getId();
        int itemsBeanId = itemsBean.getId();
        Intent intent = new Intent(this, GonglueActivity.class);
        intent.putExtra("itemsBeanId", itemsBeanId + "");
        intent.putExtra("columenId",columnId);
        //下面的参数 是被张子阳这坑比 害的 必须再传三个参数过去，说为了减少网络请求
        String title = itemsBean.getColumn().getTitle();
        String cover_image_url = itemsBean.getColumn().getCover_image_url();
        String description = itemsBean.getColumn().getDescription();
        intent.putExtra("title",title);
        intent.putExtra("cover_image_url",cover_image_url);
        intent.putExtra("description",description);


        startActivity(intent);
    }
    @Override
    public void onResponse(Call<JinxuanViewPagerBean> call, Response<JinxuanViewPagerBean> response) {
        JinxuanViewPagerBean.DataBean dataBean = response.body().getData();
        title.setText(dataBean.getTitle());
        List<JinxuanLiwuBean.DataBean.ItemsBean> itemsBeen = dataBean.getItems();
        if (itemsBeen != null) {
            listViewAdapter.addAll(itemsBeen);
        }
    }
    @Override
    public void onFailure(Call<JinxuanViewPagerBean> call, Throwable t) {

    }

    /**
     * 顶部的一些点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.shouye_jinxuan_viewpagerclick_back:
                finish();
                break;
            case R.id.shouye_jinxuan_viewpagerclick_fenxiang:
                if (sharePopupWindow != null) {
                    params.alpha = 0.7f; //0.0-1.0
                    getWindow().setAttributes(params);
                    sharePopupWindow.showAtLocation(rootView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                }
                break;
            //分享弹窗取消
            case R.id.shouye_gonglue_share_cancle:
                sharePopupWindow.dismiss();
                break;
        }
    }
}
